let db = require("../db/index");

// 业主查询
exports.ownerlist = (req, res) => {
    var sql =
        `select * from owner where owner_id=${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 添加业主
exports.owneradd = (req, res) => {
    var sql =
        `insert into owner(name,IDcard,DateofBirth,nation,Householdregistrationtype,maritalstatus,Currentresidentialaddress,phonenumber,ownertype,sex,Politicalaffiliation,Residentialtype,TemporaryResidencePermit,ContactName,radio,Contactaddress,Relationship,contactphone,Registeredresidence,Owner_state)values('${req.query.name}','${req.query.IDcard}','${req.query.DateofBirth}','${req.query.nation}','${req.query.Householdregistrationtype}','${req.query.maritalstatus}','${req.query.Currentresidentialaddress}','${req.query.phonenumber}','${req.query.ownertype}','${req.query.sex}','${req.query.Politicalaffiliation}','${req.query.Residentialtype}','${req.query.TemporaryResidencePermit}','${req.query.ContactName}','${req.query.radio}','${req.query.Contactaddress}','${req.query.Relationship}','${req.query.contactphone}','${req.query.Registeredresidence}','正常')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            data,
            state:200
        });
    });
};


// 添加业主
exports.ownerinsert = (req, res) => {
    console.log(req.body)
    var sql =
        `insert into owner(name,IDcard,DateofBirth,nation,Householdregistrationtype,maritalstatus,Currentresidentialaddress,phonenumber,ownertype,sex,Politicalaffiliation,Residentialtype,TemporaryResidencePermit,ContactName,radio,Contactaddress,Relationship,contactphone,Registeredresidence)values('${req.body.name}','${req.body.IDcard}','${req.body.DateofBirth}','${req.body.nation}','${req.body.Householdregistrationtype}','${req.body.maritalstatus}','${req.body.Currentresidentialaddress}','${req.body.phonenumber}','${req.body.ownertype}','${req.body.sex}','${req.body.Politicalaffiliation}','${req.body.Residentialtype}','${req.body.TemporaryResidencePermit}','${req.body.ContactName}','${req.body.radio}','${req.body.Contactaddress}','${req.body.Relationship}','${req.body.contactphone}','${req.body.Registeredresidence}')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            console.log(data.insertId)
            var sql =
                `UPDATE  ${req.body.dataStore} set owner_id=${data.insertId},Housingstatus="使用中",edit="解除绑定"   WHERE  date=${req.body.date};`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send({
                        data,
                        state:200
                    });
                }
                
            });
        }
    });
};
// 解除绑定
exports.ownercontact = (req, res) => {
    var sql =
        `UPDATE  ${req.body.dataStore} set owner_id=null,Housingstatus="未使用",edit="绑定业主"   WHERE  date=${req.body.date};`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from ${req.body.dataStore} `;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
        // res.send(data);
    });
};




// 业主管理信息查询
exports.ownermanngentlist = (req, res) => {
    var sql =
        `SELECT * FROM owner,housing WHERE owner.owner_id=housing.owner_id`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            // let arr=[]
            // data.forEach((item,index) => {
            //     if(item.owner_id==null){
            //         arr.push(item)
            //     }
            // });
            // res.send(arr)
            res.send(data)
        }
    });
};